/**
 * Desenvolvido pela Fábrica de Software do CESUPA.
 * Todos os direitos reservados.
 */
package br.cesupa.fabsoft.nomedoprojeto.modules.helpmodule.beans;

import br.cesupa.fabsoft.nomedoprojeto.modules.helpmodule.entities.HelpSet;
import br.cesupa.fabsoft.nomedoprojeto.modules.helpmodule.entities.HelpTopic;
import br.cesupa.fabsoft.nomedoprojeto.modules.helpmodule.services.HelpService;
import java.util.ArrayList;
import java.util.List;
import javax.faces.model.SelectItem;

/**
 *
 * @author Breno Leite
 */
public class HelpIndexBean {

    private HelpService helpService = new HelpService();
    private List<SelectItem> helpTopics = new ArrayList<SelectItem>();

    public HelpIndexBean() {
    }

    /**
     * Retrieve the disponible help pages as {@link SelectItem}. By default they
     * are ordered from a-z.
     *
     * @return a list of select item
     */
    public List<SelectItem> getHelpTopics() {
        if (helpTopics.isEmpty()) {
            iterateOverHelpSet(helpService.getTableOfContents());
        }

        return helpTopics;
    }

    private void iterateOverHelpSet(HelpSet helpSet) {
        if (!helpSet.getHelpTopics().isEmpty()) {
            for (HelpTopic helpTopic : helpSet.getHelpTopics()) {
                SelectItem selectItem = new SelectItem();
                selectItem.setValue(helpTopic);
                selectItem.setLabel(helpTopic.getTitle());
                helpTopics.add(selectItem);
            }
        }

        for (HelpSet set : helpSet.getHelpSets()) {
            iterateOverHelpSet(set);
        }
    }
}
